package org.dromara.comm_lhz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.comm_lhz.entity.AsActivityAnswerEntity;
import org.dromara.comm_lhz.entity.AsAnswerImgEntity;
import org.dromara.comm_lhz.entity.dto.AsAnswerDto;
import org.dromara.comm_lhz.entity.vo.AsActivityAnswerVo;
import org.dromara.comm_lhz.mapper.AsActivityAnswerHMapper;
import org.dromara.comm_lhz.mapper.AsAnswerImgHMapper;
import org.dromara.comm_lhz.service.AsActivityAnswerHService;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 活动回答服务实现类
 */
@Service
public class AsActivityAnswerHServiceImpl extends ServiceImpl<AsActivityAnswerHMapper, AsActivityAnswerEntity> implements AsActivityAnswerHService {

    @Autowired
    private AsAnswerImgHServiceImpl asAnswerImgHService;
    // 引入查询图片的Mapper
    @Autowired
    private AsAnswerImgHMapper asAnswerImgHMapper;

    /**
     * 添加回答
     * @param asAnswerDto 回答数据传输对象
     */
    @Override
    public void addAnswer(AsAnswerDto asAnswerDto) {
        //获取当前用户id
        asAnswerDto.setUserId(LoginHelper.getLoginUser().getUserId());

        //获取信息添加进回答表
        AsActivityAnswerEntity asActivityAnswerEntity = BeanUtil.copyProperties(asAnswerDto, AsActivityAnswerEntity.class);
        this.save(asActivityAnswerEntity);
        //将图片添加进回答图片表
        List<String> imgList = asAnswerDto.getImgList();
        imgList.forEach(img -> {
            AsAnswerImgEntity asAnswerImgEntity = new AsAnswerImgEntity();
            asAnswerImgEntity.setAnswerId(asActivityAnswerEntity.getId());
            asAnswerImgEntity.setAnswerImg(img);
            asAnswerImgHService.save(asAnswerImgEntity);
        });
    }

    /**
     * 分页查询回答
     * @param asActivityAnswerEntity 查询条件
     * @param pageable 分页信息
     * @return 分页对象，包含回答及其图片信息
     */
    @Override
    public Page<AsActivityAnswerVo> getAnswer(AsActivityAnswerEntity asActivityAnswerEntity, Pageable pageable) {
        // 创建分页请求对象，设置当前页码（从1开始）和每页大小
        Page<AsActivityAnswerEntity> pageRequest = new Page<>(pageable.getPageNumber() + 1, pageable.getPageSize());

        // 创建查询条件包装器
        LambdaQueryWrapper<AsActivityAnswerEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 只有当内容不为空时才添加模糊查询条件
        if (asActivityAnswerEntity.getContent() != null && !asActivityAnswerEntity.getContent().isEmpty()) {
            queryWrapper.like(AsActivityAnswerEntity::getContent, asActivityAnswerEntity.getContent());
        }

        // 执行分页查询
        Page<AsActivityAnswerEntity> entityPage = this.page(pageRequest, queryWrapper);

        // 转换查询结果为Vo对象，并查询图片
        Page<AsActivityAnswerVo> voPage = new Page<>(entityPage.getCurrent(), entityPage.getSize(), entityPage.getTotal());
        voPage.setRecords(entityPage.getRecords().stream().map(entity -> {
            AsActivityAnswerVo vo = new AsActivityAnswerVo();
            BeanUtils.copyProperties(entity, vo);

            // 查询图片列表并设置到Vo对象中
            List<String> imgList = asAnswerImgHMapper.selectList(new LambdaQueryWrapper<AsAnswerImgEntity>()
                    .eq(AsAnswerImgEntity::getAnswerId, entity.getId())) // 使用 answerId 作为查询条件
                    .stream()
                    .map(AsAnswerImgEntity::getAnswerImg) // 获取图片路径
                    .collect(Collectors.toList());
            vo.setImgList(imgList);

            return vo;
        }).collect(Collectors.toList()));

        return voPage;
    }

    /**
     * 更新回答
     * @param asAnswerDto 回答数据传输对象
     */
    @Override
    public void updateAnswer(AsAnswerDto asAnswerDto) {
        // 获取当前用户id
        asAnswerDto.setUserId(LoginHelper.getLoginUser().getUserId());

        // 更新回答表中的信息
        AsActivityAnswerEntity asActivityAnswerEntity = BeanUtil.copyProperties(asAnswerDto, AsActivityAnswerEntity.class);
        this.updateById(asActivityAnswerEntity);

        if (asAnswerDto.getImgList() != null) {
            // 删除旧的图片
            asAnswerImgHService.remove(new LambdaQueryWrapper<AsAnswerImgEntity>()
                    .eq(AsAnswerImgEntity::getAnswerId, asActivityAnswerEntity.getId()));

            // 将新的图片添加进回答图片表
            List<String> imgList = asAnswerDto.getImgList();
            imgList.forEach(img -> {
                AsAnswerImgEntity asAnswerImgEntity = new AsAnswerImgEntity();
                asAnswerImgEntity.setAnswerId(asActivityAnswerEntity.getId());
                asAnswerImgEntity.setAnswerImg(img);
                asAnswerImgHService.save(asAnswerImgEntity);
            });
        }
    }
}
